package org.cornland.systemWarning.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.netty.util.internal.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.cornland.systemWarning.mapper.DjdwxxMapper;
import org.cornland.systemWarning.mapper.RealdataTagMapper;
import org.cornland.systemWarning.model.Djdwxx;
import org.cornland.systemWarning.model.RealdataTag;
import org.cornland.systemWarning.model.impl.WellTags;
import org.cornland.systemWarning.service.RealdataTagService;
import org.cornland.systemWarning.utils.AppUrl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 实时数据解析点位配置表 服务实现类
 * </p>
 *
 * @author Hawk Tang
 * @since 2021-11-02
*/


@Service
@Slf4j
public class RealdataTagServiceImpl extends ServiceImpl<RealdataTagMapper, RealdataTag> implements RealdataTagService {

   @Autowired
   private RealdataTagMapper realdataTagMapper;

    @Autowired
   private DjdwxxMapper djdwxxMapper;

    @Autowired
    AppUrl appUrl;

    @Autowired
    RestTemplate restTemplate;


    public List<RealdataTag> getExistControllerTag(){
       return realdataTagMapper.getExistControllerTag();
   }

   @Cacheable(cacheNames = "welltags")
   public WellTags wellTags(String wellId){
       QueryWrapper wrapper = new QueryWrapper();
       wrapper.eq("well_id",wellId);
       List<Djdwxx> list = djdwxxMapper.selectList(wrapper);

       RealdataTag realdataTag = realdataTagMapper.selectById(wellId);

       Set<String> set = new HashSet();

       if(realdataTag != null){
           String statusTag = realdataTag.getStatusTag();
           String scheduleTag = realdataTag.getScheduleTag();
           String czylTag = realdataTag.getCzylTag();
           String dayactionTag = realdataTag.getDayactionTag();
           String oilpressureTag = realdataTag.getOilpressureTag();
           String lidpressureTag = realdataTag.getLidpressureTag();
           String instanproductionTag = realdataTag.getInstanproductionTag();

           if(!StringUtil.isNullOrEmpty(statusTag)){
               set.add(statusTag);
           }
           if(!StringUtil.isNullOrEmpty(scheduleTag)){
               set.add(scheduleTag);
           }
           if(!StringUtil.isNullOrEmpty(czylTag)){
               set.add(czylTag);
           }
           if(!StringUtil.isNullOrEmpty(dayactionTag)){
               set.add(dayactionTag);
           }
           if(!StringUtil.isNullOrEmpty(oilpressureTag)){
               set.add(oilpressureTag);
           }
           if(!StringUtil.isNullOrEmpty(lidpressureTag)){
               set.add(lidpressureTag);
           }
           if(!StringUtil.isNullOrEmpty(instanproductionTag)){
               set.add(instanproductionTag);
           }
       }


       for (int i = 0; i < list.size() ; i++) {
           Djdwxx djdwxx = list.get(i);
           String tag = djdwxx.getTag();
           if(!StringUtil.isNullOrEmpty(tag)){
               set.add(tag);
           }
       }


       String join = StringUtils.join(set, ",");
       WellTags wellTags = new WellTags();
       wellTags.setTags(join);

       return wellTags;
   }

   @Cacheable(cacheNames = "tagsRequest")
   public JSONArray getTagsRequestWithCache(String tags){
       System.out.println("进getTagsRequestWithCache");
       String url = appUrl.queryRealTimeDataUrl + "?tags=" + tags;
       URI uri = URI.create(url);
       String tagString = restTemplate.getForObject(uri, String.class);

       // 调用实时数据接口 返回jsonArray
       return JSONArray.parseArray(tagString);

   }

    @CachePut(cacheNames = "tagsRequest")
    public JSONArray updateTagsRequestWithCache(String tags){
//        System.out.println("进updateTagsRequestWithCache");
        String url = appUrl.queryRealTimeDataUrl + "?tags=" + tags;
        URI uri = URI.create(url);
        String tagString = restTemplate.getForObject(uri, String.class);

        // 调用实时数据接口 返回jsonArray
        return JSONArray.parseArray(tagString);

    }

   public RealdataTag selectByPrimaryKey(Integer id){
        RealdataTag realdataTag = realdataTagMapper.selectById(id);
        return realdataTag;
   }

   public List<RealdataTag> selectAll(){
        List<RealdataTag> realdataTagList = realdataTagMapper.selectList(null);
        return realdataTagList;
   }

   public Integer insert(RealdataTag realdataTag){
        int result = realdataTagMapper.insert(realdataTag);
        return result;
   }

   public Integer updateByPrimaryKey(RealdataTag realdataTag){
        int result = realdataTagMapper.updateById(realdataTag);
        return result;
   }

   public Integer deleteByPrimaryKey(Integer id){
        int result = realdataTagMapper.deleteById(id);
        return result;
   }







}